﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsFlower
{
    public static class AddFlower
    {
        public static void AddPetalEffectToForm(this Form form)
        {
            if (form == null) return;

            // 创建代理类来设置双缓冲
            SetDoubleBuffered(form);

            List<Petal> petals = new List<Petal>();
            Timer petalTimer = new Timer();
            Random random = new Random();

            // 设置定时器
            petalTimer.Interval = 30;
            petalTimer.Tick += (s, e) =>
            {
                // 随机生成新花瓣
                if (random.Next(100) < 15)
                {
                    int startX = random.Next(form.ClientSize.Width);
                    Petal newPetal = new Petal(new Point(startX, -20), random);
                    petals.Add(newPetal);
                }

                // 更新花瓣位置
                UpdatePetals(petals, form.ClientRectangle);

                // 触发重绘
                form.Invalidate();
            };

            // 绘制花瓣
            form.Paint += (s, e) =>
            {
                foreach (var petal in petals)
                {
                    DrawPetalOnForm(e.Graphics, petal);
                }
            };

            // 窗体大小改变时重新调整
            form.Resize += (s, e) =>
            {
                form.Invalidate();
            };

            // 清理资源
            form.FormClosed += (s, e) =>
            {
                petalTimer.Stop();
                petalTimer.Dispose();
                petals.Clear();
            };

            petalTimer.Start();

            // 初始花瓣
            for (int i = 0; i < 5; i++)
            {
                int startX = random.Next(form.ClientSize.Width);
                petals.Add(new Petal(new Point(startX, -20), random));
            }
        }

        // 使用反射来设置双缓冲（绕过访问限制）
        private static void SetDoubleBuffered(Control control)
        {
            // 使用反射来调用 protected 的 SetStyle 方法
            typeof(Control).GetMethod("SetStyle",
                System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance)
                ?.Invoke(control, new object[] { ControlStyles.DoubleBuffer |
                                               ControlStyles.UserPaint |
                                               ControlStyles.AllPaintingInWmPaint, true });
        }

        private static void UpdatePetals(List<Petal> petals, Rectangle bounds)
        {
            for (int i = petals.Count - 1; i >= 0; i--)
            {
                petals[i].Update();
                if (petals[i].IsOutOfBounds(bounds))
                {
                    petals.RemoveAt(i);
                }
            }
        }

        private static void DrawPetalOnForm(Graphics g, Petal petal)
        {
            // 简化绘制方法
            using (var brush = new SolidBrush(Color.FromArgb(180, petal.Color)))
            {
                g.FillEllipse(brush, petal.Position.X, petal.Position.Y,
                             petal.Size.Width, petal.Size.Height);
            }
        }
    }
}